package sz.szsmk.citizencard.mvp.presenter;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback;
import sz.szsmk.citizencard.ui.base.IBaseContact;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

import sz.szsmk.citizencard.retrofitrxjava.newversion.SubscriberCallBack;

/**
 * Created by wangzhongfu on 2017/6/6.
 */

public class IBasePresenter<T extends IBaseContact.IBaseView>implements IBaseContact.IBasePresenter<T> {
    protected T mView;
    private CompositeSubscription mCompositeSubscription;

    @Override
    public void attachView(T view) {
        this.mView = view;

    }


    @Override
    public void detachView() {
        mView = null;
        if(mCompositeSubscription!=null){
            mCompositeSubscription.unsubscribe();
        }
        mCompositeSubscription=null;
    }

    protected void addSubscription(Subscription subscription){
        if (null == mCompositeSubscription){
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(subscription);
    }

    public boolean isViewAttached(){
        return mView != null;
    }

    public T getMvpView(){
        return mView;
    }

    public <T> void addSubscription(Observable weather , ApiCallback<T> apiCallback ) {
        addSubscription(weather
                .subscribeOn(Schedulers.io()) //被观察者observable是在io线程里处理的
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberCallBack(apiCallback)));
    }

}
